<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\StoresRepositories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StoresController extends Controller
{
    public function __construct(StoresRepositories $storesRepositories)
    {
        $this->storesRepositories = $storesRepositories;

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.stores.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.stores.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name'  => 'required|string',
            'logo'  => 'required|string',
        ],[
            'name.required'=>'* 名称必传!',
            'logo.required'=>'*logo必传!',
        ]);
        $ret = $this->storesRepositories->save($request->all());

        if($ret)
        {
            return redirect()->route('admin.stores')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,Request $request)
    {
        $info = $this->storesRepositories->find($id);
        return view('admin.stores.edit',compact('info'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request,[
            'name'  => 'required|string',
            'logo'  => 'required|string',
        ],[
            'name.required'=>'* 名称必传!',
            'logo.required'=>'*logo必传!',
        ]);
        $ret = $this->storesRepositories->update($id,$request->all());

        if($ret)
        {
            return redirect()->route('admin.stores')->with(['success'=>'更新成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $ids=request()->input('ids');

        if(empty($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '请选择删除项！',
            ]);
        }

        if(!is_array($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '非法参数！',
            ]);
        }

        $ret =$this->storesRepositories->delete($ids);
        if($ret)
        {
            return response()->json([
                'code' => 0,
                'msg'   => '删除成功！',
            ]);
        }
        return response()->json([
            'code' => -1,
            'msg'   => '删除失败！',
        ]);
    }
    public function data(Request $request)
    {
        $data = $request->all();
        $res = $this->storesRepositories->search($data);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
}
